#!/bin/bash

# Replace with your container name
CONTAINER_NAME="influxdb"

# Function to check if a docker container is running
is_container_running() {
    container_name=$1
    if [ "$(docker ps -q -f name=^/${container_name}$)" ]; then
        return 0
    else
        return 1
    fi
}

# Step 1: Check for Docker
if ! command -v docker &> /dev/null; then
    echo "Docker is not installed. Please install Docker and try again."
    exit 1
fi

# Step 2: Check for Docker Compose
if ! command -v docker-compose &> /dev/null; then
    echo "Docker Compose is not installed. Please install Docker Compose and try again."
    exit 1
fi

# Step 3: Check if specified Docker container is running
if is_container_running $CONTAINER_NAME; then
    echo "Docker container '$CONTAINER_NAME' is already running."
    exit 1
else
    echo "Docker container '$CONTAINER_NAME' is not running. Proceeding..."
fi

# Step 4: Start the application using Docker Compose
echo "Starting the application with Docker Compose..."
docker-compose up -d

if [ $? -ne 0 ]; then
    echo "Failed to start the application with Docker Compose."
    exit 1
fi

## Step 5: Wait for MariaDB to be ready
#echo "Waiting for influxdb to be ready..."
#RETRIES=30
#until docker exec $CONTAINER_NAME mysqladmin ping -h "127.0.0.1" --silent; do
#    RETRIES=$((RETRIES-1))
#    if [ $RETRIES -le 0 ]; then
#        echo "MariaDB did not become ready in time."
#        exit 1
#    fi
#    echo -n "."
#    sleep 2
#done
#
#echo "MariaDB is ready and accepting connections."
#
## Print the current server IP address
#IP_ADDRESS=$(hostname -I | awk '{print $1}')
#
#echo "================================================"
#echo "'$CONTAINER_NAME' installation successful"
#echo "MariaDB Host: $IP_ADDRESS"
#echo "MariaDB Port: 3306"
#echo "MariaDB User: root"
#echo "MariaDB Pwd : Hzsun@310012"
#echo "================================================"